// 引入egg的Controller类
const Controller = require("egg").Controller;


let fruitList=["香蕉","苹果","鸭梨"];

//定义newsController用于继承egg的Controller
class NewsController extends Controller{

    async index(){
        this.ctx.body=fruitList;
    }

    // 添加页面
    async new(){
        this.ctx.body=`
            <form method='post' action='/fruits'>
                <input name='fruitname'>
                <button>添加</button>
            </form>
        `;
    }

    // 添加方法
    async create(){
        let fruit=this.ctx.request.body;
        fruitList.push(fruit.fruitname);
        
        // 重定向到get请求下的/fruits路径
        this.ctx.redirect('/fruits');
    }
}


// 暴露NewsController
module.exports = NewsController;